package hall.app.customwidgets.library.client.tab;

import hall.app.customwidgets.library.client.listwidgets.ListDefinition;
import hall.app.customwidgets.library.client.listwidgets.ListItem;
import hall.app.customwidgets.library.client.listwidgets.UnOrderedList;

import java.util.List;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;

public class TabbedPanel extends Composite
{

	FlowPanel flowPanel = new FlowPanel(); 
	UnOrderedList unOrderedList = new UnOrderedList();
	
	public TabbedPanel() {
		initWidget(flowPanel);

		flowPanel.getElement().setAttribute("class", "column span-24 last");
		flowPanel.getElement().setAttribute("id", "tab-set");
		
		unOrderedList.getElement().setAttribute("class", "tabs");
		flowPanel.add(unOrderedList);
	}
	
	public void addListItems(List<ListDefinition> listDefinitions)
	{
		for(ListDefinition listDefinition : listDefinitions)
			unOrderedList.add(createAListItem(listDefinition.getTitle(), listDefinition.getTarget(), listDefinition.isSelected()));
	}
	
	private ListItem createAListItem(String title, String target, boolean isSelected)
	{
		ListItem listItem = new ListItem();
		
		final Anchor anchor = new Anchor();
		anchor.setText(title);
		anchor.setHref(target);
		
		HTMLPanel htmlPanel = new HTMLPanel(title + " " + target);
		htmlPanel.getElement().setAttribute("id", target.replace("#", ""));
		htmlPanel.setVisible(false);
		
		if(isSelected)
		{
			anchor.getElement().setAttribute("class", "selected");
			htmlPanel.setVisible(true);	
		}
		
		flowPanel.add(htmlPanel);
		
		listItem.add(anchor);
		return listItem;
	}
	
}
